package com.controller.agent;

import com.biz.BdTeamCodeBiz;
import com.biz.BdTeamCodeLogBiz;
import com.controller.backend.BaseController;
import com.core.Result;
import com.core.ResultGenerator;
import com.entity.model.BdTeamCode;
import com.entity.model.BdTeamCodeLog;
import com.util.Random;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by jc on 2019/11/23.
 */
@Controller
@RequestMapping("/agent/security/bdTeamCode")
public class AgentBdTeamCodeController extends BaseController<BdTeamCode> {
    @Autowired
    private BdTeamCodeBiz bdTeamCodeBiz;

    @Autowired
    private BdTeamCodeLogBiz bdTeamCodeLogBiz;

    @RequestMapping(value = "detail")
    public String detail() throws Exception {
        return "/agent/security/bdTeam/codeLogForm";
    }

    @RequestMapping(value = "codeLogInfo")
    public String codeLogInfo() throws Exception {
        return "/agent/security/bdTeam/codeLogInfoForm";
    }

    @RequestMapping(value = "addCodeLog")
    @ResponseBody
    public Result addCodeLog(BdTeamCode bdTeamCode) throws Exception {
        if (bdTeamCode.getAmount() <= 0) {
            return ResultGenerator.genFailResult("兑换码数量有误");
        }
        bdTeamCodeBiz.save(bdTeamCode);

        BdTeamCodeLog bdTeamCodeLog = new BdTeamCodeLog();
        for (Integer i = 0; i < bdTeamCode.getAmount(); i++) {
            String code = Random.getRandomStr();
            bdTeamCodeLog.setBdTeamCodeId(bdTeamCode.getId());
            bdTeamCodeLog.setCode(code);
            bdTeamCodeLog.setStatus(0);
            bdTeamCodeLogBiz.save(bdTeamCodeLog);
            bdTeamCodeLog.setId(null);
        }
        return ResultGenerator.genSuccessResult();
    }
}
